﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Library;
using Library.Models;
using Library.MVCHelpers;

namespace Ecommerce.Models
{
    public class EditUserViewModel
    {
        public EditUserViewModel()
        { }

        public EditUserViewModel(ApplicationUser user)
        {
            Id = user.Id;
            Email = user.Email;
            FullName = user.FullName;
            PhoneNumber = user.PhoneNumber;
            Gender = user.Gender;
            Birthday = user.Birthday;
            PositionInOrganization = user.PositionInOrganization;
        }

        public string Id { get; set; }

        [CultureRequired]
        [CultureEmailAddress]
        public string Email { get; set; }

        [CultureMaxLength(256)]
        public string FullName { get; set; }

        [CulturePhone]
        public string PhoneNumber { get; set; }

        public byte? Gender { get; set; }

        [DataType(DataType.Date)]
        [CultureDateDisplayFormat]
        public DateTime? Birthday { get; set; }

        [CultureMaxLength(256)]
        public string PositionInOrganization { get; set; }
    }

    public class CreateUserViewModel : EditUserViewModel
    {
        public CreateUserViewModel()
            : base()
        { }

        public CreateUserViewModel(ApplicationUser user)
            : base(user)
        { }

        [CultureRequired]
        [DataType(DataType.Password)]
        [CultureMaxLength(100)]
        [CultureMinLength(8)]
        public string Password { get; set; }

        [Display(Name = "Confirm password")]
        [CultureRequired]
        [DataType(DataType.Password)]
        [CultureMaxLength(100)]
        [CultureMinLength(8)]
        [CultureCompare("Password")]
        public string ConfirmPassword { get; set; }
    }

    public class ChangeUserPasswordModel
    {
        public ChangeUserPasswordModel()
        { }

        public ChangeUserPasswordModel(ApplicationUser user)
        {
            Id = user.Id;
        }

        public string Id { get; set; }

        [CultureRequired]
        [DataType(DataType.Password)]
        [CultureMaxLength(100)]
        [CultureMinLength(8)]
        public string Password { get; set; }

        [Display(Name = "Confirm password")]
        [CultureRequired]
        [DataType(DataType.Password)]
        [CultureMaxLength(100)]
        [CultureMinLength(8)]
        [CultureCompare("Password")]
        public string ConfirmPassword { get; set; }
    }

    public class EditRoleViewModel
    {
        public EditRoleViewModel()
        {

        }

        public EditRoleViewModel(ApplicationRole role)
        {
            Id = role.Id;
            Name = role.Name;
            Description = role.Description;
        }

        [CultureMaxLength(128)]
        public string Id { get; set; }

        [CultureRequired]
        [CultureMaxLength(256)]
        [Display(Name = "Role name")]
        public string Name { get; set; }

        [CultureMaxLength(512)]
        public string Description { get; set; }
    }

    public class EditProductCategoryViewModel
    {
        public EditProductCategoryViewModel()
        { }

        public EditProductCategoryViewModel(ProductCategory productCategory, string culture)
        {
            Id = productCategory.Id;
            ParentId = productCategory.ParentId;

            Culture = culture;

            var productCategoryCulture = productCategory.ProductCategoryCultures.SingleOrDefault(pcc => pcc.Culture == culture);
            if (productCategoryCulture != null)
            {
                Name = productCategoryCulture.Name;
                Description = productCategoryCulture.Description;
                Keywords = productCategoryCulture.Keywords;
                WebTitle = productCategoryCulture.WebTitle;
                WebDescription = productCategoryCulture.WebDescription;
                Url = productCategoryCulture.Url;
            }
        }
        public int Id { get; set; }
        public int? ParentId { get; set; }
        public string Culture { get; set; }

        [CultureRequired]
        [CultureMaxLength(50)]
        [Display(Name = "Category name")]
        public string Name { get; set; }

        [CultureMaxLength(512)]
        public string Description { get; set; }

        [CultureRequired]
        [CultureMaxLength(150)]
        public string Keywords { get; set; }

        [CultureRequired]
        [CultureMaxLength(150)]
        [Display(Name = "Webpage title")]
        public string WebTitle { get; set; }

        [CultureRequired]
        [CultureMaxLength(200)]
        [Display(Name = "Webpage description")]
        public string WebDescription { get; set; }

        [CultureRequired]
        public string Url { get; set; }

        public static List<ProductCategory> GetChildsOfProductCategory(ProductCategory productCategory)
        {
            if (productCategory == null)
            {
                return null;
            }

            if (productCategory.ProductCategories1.Count == 0)
            {
                return null;
            }

            var result = new List<ProductCategory>();
            result.AddRange(productCategory.ProductCategories1);
            foreach (var pc in productCategory.ProductCategories1)
            {
                var childs = GetChildsOfProductCategory(pc);
                if (childs != null)
                {
                    result.AddRange(childs);
                }
            }

            return result;
        }
    }

    public class EditProductViewModel
    {
        public EditProductViewModel()
        { }

        public EditProductViewModel(Product product, string culture)
        {
            Id = product.Id;
            ProductCategoryId = product.ProductCategoryId;
            BrandId = product.BrandId;
            PriceDisplay = product.PriceDisplay;
            StrikethroughPriceDisplay = product.StrikethroughPriceDisplay;
            Status = product.Status.HasValue ? (ProductStatuses)product.Status : ProductStatuses.Undefined;

            Culture = culture;
            var productCulture = product.ProductCultures.SingleOrDefault(i => i.Culture == culture);
            if (productCulture != null)
            {
                Name = productCulture.Name;
                Description = productCulture.Description;
                Keywords = productCulture.Keywords;
                WebTitle = productCulture.WebTitle;
                WebDescription = productCulture.WebDescription;
                Url = productCulture.Url;
                SmallArticle = productCulture.SmallArticle;
                Article = productCulture.Article;
                Policies = productCulture.Policies;
                PromotionCaption = productCulture.PromotionCaption;
                PromotionDetails = productCulture.PromotionDetails;
                BannerCaption = productCulture.BannerCaption;
                BannerAlt = productCulture.BannerAlt;
                BannerUrl = productCulture.BannerUrl;
                BannerSrc = productCulture.BannerSrc;
            }
        }



        public int Id { get; set; }

        [CultureRequired]
        [Display(Name = "Product category")]
        public int ProductCategoryId { get; set; }

        [CultureRequired]
        [Display(Name = "Brand")]
        public int BrandId { get; set; }
        public string PriceDisplay { get; set; }
        public string StrikethroughPriceDisplay { get; set; }
        public ProductStatuses Status { get; set; }

        public string Culture { get; set; }

        [CultureRequired]
        [CultureMaxLength(50)]
        [Display(Name = "Product name")]
        public string Name { get; set; }

        [CultureMaxLength(512)]
        public string Description { get; set; }

        [CultureRequired]
        [CultureMaxLength(150)]
        public string Keywords { get; set; }

        [CultureRequired]
        [CultureMaxLength(150)]
        [Display(Name = "Webpage title")]
        public string WebTitle { get; set; }

        [CultureRequired]
        [CultureMaxLength(200)]
        [Display(Name = "Webpage description")]
        public string WebDescription { get; set; }

        [CultureRequired]
        public string Url { get; set; }

        [AllowHtml]
        public string SmallArticle { get; set; }

        [AllowHtml]
        public string Article { get; set; }

        [AllowHtml]
        public string Policies { get; set; }

        [AllowHtml]
        public string PromotionCaption { get; set; }

        [AllowHtml]
        public string PromotionDetails { get; set; }

        public string BannerUrl { get; set; }
        public string BannerCaption { get; set; }
        public string BannerAlt { get; set; }
        public string BannerSrc { get; set; }

        [DataType(DataType.Upload)]
        public HttpPostedFileBase Banner { get; set; }
    }

    public class ProductImageViewModel : ImageViewModel
    {
        public ProductImageViewModel() { }

        public ProductImageViewModel(int productId, string culture, ProductImage imageModel)
        {
            ProductId = productId;
            Culture = culture;
            IsAvatar = imageModel.IsAvatar ?? false;
            Src = imageModel.Src;
            AltText = imageModel.AltText;
            Caption = imageModel.Caption;
            Width = imageModel.Width;
            Height = imageModel.Height;
        }

        public string Culture { get; set; }
        public int ProductId { get; set; }
        public bool IsAvatar { get; set; }
    }

    public class EditSpecificationGroupViewModel
    {
        public EditSpecificationGroupViewModel() { }
        public EditSpecificationGroupViewModel(SpecificationGroup specificationGroup, string culture)
        {
            Id = specificationGroup.Id;
            ProductCategoryId = specificationGroup.ProductCategoryId;
            DisplayOrder = specificationGroup.DisplayOrder;

            Culture = culture;

            var sgCulture = specificationGroup.SpecificationGroupCultures.SingleOrDefault(i => i.Culture == culture);
            if (sgCulture != null)
            {
                Name = sgCulture.Name;
                Description = sgCulture.Description;
            }
        }

        public int Id { get; set; }
        public int ProductCategoryId { get; set; }
        public string Culture { get; set; }

        [Display(Name = "Display order")]
        public int? DisplayOrder { get; set; }

        [CultureRequired]
        [CultureMaxLength(50)]
        [Display(Name = "Specification group name")]
        public string Name { get; set; }

        [CultureMaxLength(512)]
        public string Description { get; set; }
    }

    public class EditSpecificationViewModel
    {
        public EditSpecificationViewModel()
        { }

        public EditSpecificationViewModel(Specification specification, string culture)
        {
            Id = specification.Id;
            Culture = culture;
            SpecificationGroupId = specification.SpecificationGroupId;
            DisplayOrder = specification.DisplayOrder;

            var modelCulture = specification.SpecificationCultures.SingleOrDefault(i => i.Culture == culture);
            if (modelCulture != null)
            {
                Name = modelCulture.Name;
                Description = modelCulture.Description;
            }
        }

        public int Id { get; set; }

        public string Culture { get; set; }

        public int SpecificationGroupId { get; set; }

        [Display(Name = "Display order")]
        public int? DisplayOrder { get; set; }

        [CultureRequired]
        [CultureMaxLength(50)]
        [Display(Name = "Specification name")]
        public string Name { get; set; }

        [CultureMaxLength(512)]
        public string Description { get; set; }
    }

    public class EditSpecificationValueViewModel
    {
        public EditSpecificationValueViewModel()
        {

        }

        public EditSpecificationValueViewModel(SpecificationValue specificationValue)
        {
            Id = specificationValue.Id;
            Value = specificationValue.Value;
            Weight = specificationValue.Weight;
            SpecificationId = specificationValue.SpecificationId;
        }

        public int Id { get; set; }

        public int SpecificationId { get; set; }

        [CultureRequired]
        [CultureMaxLength(128)]
        [Display(Name = "Specification value")]
        public string Value { get; set; }

        [Display(Name = "Weight value")]
        public int? Weight { get; set; }
    }

    public class EditBrandViewModel
    {
        public EditBrandViewModel()
        { }

        public EditBrandViewModel(Brand brand, string culture)
        {
            Id = brand.Id;

            Culture = culture;

            var brandCulture = brand.BrandCultures.SingleOrDefault(pcc => pcc.Culture == culture);
            if (brandCulture != null)
            {
                Name = brandCulture.Name;
                Description = brandCulture.Description;
                Keywords = brandCulture.Keywords;
                WebTitle = brandCulture.WebTitle;
                WebDescription = brandCulture.WebDescription;
                Url = brandCulture.Url;
            }
        }
        public int Id { get; set; }
        public string Culture { get; set; }

        [CultureRequired]
        [CultureMaxLength(50)]
        [Display(Name = "Brand name")]
        public string Name { get; set; }

        public string Description { get; set; }

        [CultureRequired]
        [CultureMaxLength(150)]
        public string Keywords { get; set; }

        [CultureRequired]
        [CultureMaxLength(150)]
        [Display(Name = "Webpage title")]
        public string WebTitle { get; set; }

        [CultureRequired]
        [CultureMaxLength(200)]
        [Display(Name = "Webpage description")]
        public string WebDescription { get; set; }

        [CultureRequired]
        public string Url { get; set; }
    }

    public class EditPageGroupViewModel
    {
        public EditPageGroupViewModel()
        { }

        public EditPageGroupViewModel(PageGroup pageGroup, string culture)
        {
            Id = pageGroup.Id;

            Culture = culture;

            var pgCulture = pageGroup.PageGroupCultures.SingleOrDefault(pcc => pcc.Culture == culture);
            if (pgCulture != null)
            {
                Name = pgCulture.Name;
                Description = pgCulture.Description;
                Keywords = pgCulture.Keywords;
                WebTitle = pgCulture.WebTitle;
                WebDescription = pgCulture.WebDescription;
                Url = pgCulture.Url;
            }
        }
        public int Id { get; set; }
        public string Culture { get; set; }

        [CultureRequired]
        [CultureMaxLength(50)]
        [Display(Name = "Page group name")]
        public string Name { get; set; }

        public string Description { get; set; }

        [CultureRequired]
        [CultureMaxLength(150)]
        public string Keywords { get; set; }

        [CultureRequired]
        [CultureMaxLength(150)]
        [Display(Name = "Webpage title")]
        public string WebTitle { get; set; }

        [CultureRequired]
        [CultureMaxLength(200)]
        [Display(Name = "Webpage description")]
        public string WebDescription { get; set; }

        [CultureRequired]
        public string Url { get; set; }
    }

    public class EditPageViewModel
    {
        public EditPageViewModel() { }

        public EditPageViewModel(Page page, string culture)
        {
            Id = page.Id;
            PageGroupId = page.PageGroupId;
            Culture = culture;

            var pageCulture = page.PageCultures.SingleOrDefault(pc => pc.Culture == culture);
            if (pageCulture != null)
            {
                Content = pageCulture.Content;
                Keywords = pageCulture.Keywords;
                WebTitle = pageCulture.WebTitle;
                WebDescription = pageCulture.WebDescription;
                Url = pageCulture.Url;
            }
        }

        public int Id { get; set; }
        public int? PageGroupId { get; set; }
        public string Culture { get; set; }

        [AllowHtml]
        public string Content { get; set; }

        [CultureRequired]
        [CultureMaxLength(150)]
        public string Keywords { get; set; }

        [CultureRequired]
        [CultureMaxLength(150)]
        [Display(Name = "Webpage title")]
        public string WebTitle { get; set; }

        [CultureRequired]
        [CultureMaxLength(200)]
        [Display(Name = "Webpage description")]
        public string WebDescription { get; set; }

        [CultureRequired]
        [CultureMaxLength(150)]
        public string Url { get; set; }
    }

    public class PageImageViewModel : ImageViewModel
    {
        public PageImageViewModel() { }

        public PageImageViewModel(int pageId, string culture, PageImage imageModel)
        {
            PageId = pageId;
            Culture = culture;
            IsExtraSmallAvatar = imageModel.IsExtraSmallAvatar ?? false;
            IsSmallAvatar = imageModel.IsSmallAvatar ?? false;
            IsMediumAvatar = imageModel.IsMediumAvatar ?? false;
            IsLargeAvatar = imageModel.IsLargeAvatar ?? false;
            Src = imageModel.Src;
            AltText = imageModel.AltText;
            Caption = imageModel.Caption;
            Width = imageModel.Width;
            Height = imageModel.Height;
        }

        public string Culture { get; set; }
        public int PageId { get; set; }
        public bool IsExtraSmallAvatar { get; set; }
        public bool IsSmallAvatar { get; set; }
        public bool IsMediumAvatar { get; set; }
        public bool IsLargeAvatar { get; set; }
    }

    public class SettingsViewModel
    {
        public SettingsViewModel()
        {
            CompanyName = Settings.GetSetting("CompanyName");
            Copyright = Settings.GetSetting("Copyright");
            Keywords = Settings.GetSetting("Keywords");
            Description = Settings.GetSetting("Description");
        }

        [CultureRequired]
        [Display(Name = "Company name")]
        public string CompanyName { get; set; }

        [CultureRequired]
        public string Copyright { get; set; }

        [CultureRequired]
        [CultureMaxLength(150)]
        public string Keywords { get; set; }

        [CultureRequired]
        [CultureMaxLength(200)]
        public string Description { get; set; }
    }
}